package com.site.springboot.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.site.springboot.core.dao.NewsCategoryMapper;
import com.site.springboot.core.entity.NewsCategory;
import com.site.springboot.core.service.CategoryService;
import com.site.springboot.core.util.PageQueryUtil;
import com.site.springboot.core.util.PageResult;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<NewsCategoryMapper, NewsCategory> implements CategoryService{

    @Resource
    private NewsCategoryMapper newsCategoryMapper;

    @Override
    public List<NewsCategory> getAllCategories() {
        QueryWrapper<NewsCategory> querynewsCategory = new QueryWrapper<>();
        querynewsCategory.eq("is_deleted", 0)
                .orderByDesc("category_id");
        return newsCategoryMapper.selectList(querynewsCategory);
    }

    @Override
    public NewsCategory queryById(Long id) {
        return newsCategoryMapper.selectById(id);
    }

    @Override
    public PageResult getCategoryPage(PageQueryUtil pageUtil) {
        QueryWrapper<NewsCategory> newsCategoryQueryWrapper = new QueryWrapper<>();
        newsCategoryQueryWrapper.eq("is_deleted", 0)
                .orderByDesc("category_id");
        //运用mybatisplus进行分页查询
        Page<NewsCategory> categoryPage=selectPage(pageUtil.getPage(),pageUtil.getLimit(),newsCategoryQueryWrapper);
        // 获取总记录数
        int total = Math.toIntExact(newsCategoryMapper.selectCount(newsCategoryQueryWrapper));
        PageResult pageResult = new PageResult(categoryPage.getRecords(), total, pageUtil.getLimit(), pageUtil.getPage());
        return pageResult;
    }

    @Override
    public Boolean saveCategory(String categoryName) {
        /**
         * 查询是否已存在
         */
        QueryWrapper<NewsCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("category_name", categoryName);
        NewsCategory category = newsCategoryMapper.selectOne(wrapper);
        if (category == null) {
            NewsCategory newsCategory = new NewsCategory();
            newsCategory.setCategoryName(categoryName);
            newsCategory.setIsDeleted((byte) 0);
            return newsCategoryMapper.insert(newsCategory)>0;
        }
        return false;
    }

    @Override
    public Boolean updateCategory(Long categoryId, String categoryName) {
        QueryWrapper<NewsCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id", categoryId);
        NewsCategory newsCategory = newsCategoryMapper.selectOne(wrapper);
        if (newsCategory != null) {
            newsCategory.setCategoryName(categoryName);
            return  newsCategoryMapper.update(newsCategory, wrapper)>0;
        }
        return false;
    }

    @Override
    public Boolean deleteBatchByIds(Integer[] ids) {
        if (ids.length < 1) {
            return false;
        }
        //删除分类数据
        return  newsCategoryMapper.deleteBatchIds(Arrays.asList(ids))>0;
    }

    public Page<NewsCategory> selectPage(int page, int limit, QueryWrapper<NewsCategory> newsCategoryQueryWrapper) {
        Page<NewsCategory> newscategoryPage = new Page<>(page,limit);
        Page<NewsCategory> categoryPage = newsCategoryMapper.selectPage(newscategoryPage, newsCategoryQueryWrapper);
        return  categoryPage;
    }
}
